package cemp.service;

import cemp.bean.OOMObject;
import cemp.common.api.reponse.RestCommonResponse;
import cemp.common.api.reponse.RestStockInfoTodayResponse;
import cemp.common.api.reponse.RestStockPlates;
import cemp.common.api.reponse.RestStockPriceResponse;
import cemp.common.dto.response.CommonOption;
import cemp.common.dto.response.GnStatusDto;
import cemp.common.util.DateUtil;
import cemp.common.util.StockCopyPropertyUtil;
import cemp.common.util.StockUtil;
import cemp.entity.*;
import cemp.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class StockQueryService {

    private final StockThsGnMapper gnMapper;
    private final StockThsGnCurrentInfoMapper currentInfoMapper;
    private final StockUtil stockUtil;

    public Map<String, GnStatusDto> getGnUpRatio(String beginTime, String endTime){
        LambdaQueryWrapper<StockThsGnCurrentInfo> lqw = new LambdaQueryWrapper<>();
        lqw.gt(StockThsGnCurrentInfo::getStaDatetime,beginTime);
        lqw.lt(StockThsGnCurrentInfo::getStaDatetime,endTime);
        List<StockThsGnCurrentInfo> list = currentInfoMapper.selectList(lqw);
        Map<String,List<StockThsGnCurrentInfo>> gnListMap =  list.stream().collect(Collectors.groupingBy(StockThsGnCurrentInfo::getGnCode));
        Map<String, GnStatusDto> ratioMap = new HashMap<>();
        gnListMap.keySet().forEach(new Consumer<String>() {
            @Override
            public void accept(String gnCode) {
                List<StockThsGnCurrentInfo> details =  gnListMap.get(gnCode);
                GnStatusDto statusDto = new GnStatusDto(
                        gnCode,
                        details.get(0).getGnName(),
                        stockUtil.getUpRate(details.get(0).getPriceCurrent(), details.get(details.size()-1).getPriceCurrent()));
                ratioMap.put(gnCode,statusDto);
            }
        });
        return ratioMap;

    }


}
